angular.module('backofficeApp.dosomething.controllers', [])
    .controller('dosth_findCompatriotsCtrl', ['$scope','$http', '$location', 'CommunityService', function ($scope, $http, $location, communityService) {

        var commId = $("#community_select")[0].dataset.cId;
        $scope.communityId = commId;

        $scope.currentPage = 1;
        $scope.totalPage = 10;
        $scope.pageSize = 10;
        $scope.pages = [];

        $scope.communityLabelList = communityService.getAllCommunityService();
        var setIn = setInterval(function(){
            if($scope.communityLabelList){
                clearInterval(setIn);
                var selectList = [];
                $scope.communityLabelList.data.forEach(function(item){
                    var a = {};
                    a.name = item.communityName;
                    a.id = item.objId;
                    selectList.push(a);
                });
                new select({data:selectList,id:"community_select"});
            }
        },500);


        $scope.searchAction = function () {

            commId = $("#community_select")[0].dataset.cId;
            $scope.communityId = commId;

            var option = {'communityId': commId};

            $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/dosth/findCompatriots/' + $scope.currentPage + '/' + $scope.pageSize, option, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {

                var result = data.data.list;
                $scope.totalPage = data.data.totalPage;
                $scope.list = result;

            }).error(function () {
                $scope.list = null;
            });
        };


        $scope.onSelectPage = function (page) {
            $scope.currentPage = page;
            $scope.searchAction();
        };

        $scope.searchAction();

    }]).controller('dosth_findPlaymates_Ctrl', ['$scope','$http', '$location', 'CommunityService', function ($scope, $http, $location, communityService) {

        $scope.sex = "all";

        var commId = $("#community_select")[0].dataset.cId;
        var sex = $("#male_select").val();
        $scope.communityId = commId;

        $scope.currentPage = 1;
        $scope.totalPage = 10;
        $scope.pageSize = 10;
        $scope.pages = [];

        $scope.communityLabelList = communityService.getAllCommunityService();
        var setIn = setInterval(function(){
            if($scope.communityLabelList){
                clearInterval(setIn);
                var selectList = [];
                $scope.communityLabelList.data.forEach(function(item){
                    var a = {};
                    a.name = item.communityName;
                    a.id = item.objId;
                    selectList.push(a);
                });
                new select({data: selectList, id: "community_select"});
            }
        }, 500);


        $scope.selectMale = function () {
            $scope.searchAction();
        };


        $scope.searchAction = function () {

            commId = $("#community_select")[0].dataset.cId;
            sex = $("#male_select").val();
            $scope.communityId = commId;
            $scope.sex = sex;

            var option = {'communityId': commId, 'sex': sex};

            $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/dosth/findPlaymates/' + $scope.currentPage + '/' + $scope.pageSize, option, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {

                var result = data.data.list;
                $scope.totalPage = data.data.totalPage;
                $scope.list = result;

            }).error(function () {
                $scope.list = null;
            });
        };


        $scope.onSelectPage = function (page) {
            $scope.currentPage = page;
            $scope.searchAction();
        };

        $scope.searchAction();

    }]).controller('dosth_dosthTogether_Ctrl', ['$scope','$http', '$location', 'CommunityService', function ($scope, $http, $location, communityService) {

        var commId = $("#community_select")[0].dataset.cId;
        $scope.communityId = commId;

        $scope.currentPage = 1;
        $scope.totalPage = 10;
        $scope.pageSize = 10;
        $scope.pages = [];

        $scope.communityLabelList = communityService.getAllCommunityService();
        var setIn = setInterval(function(){
            if($scope.communityLabelList){
                clearInterval(setIn);
                var selectList = [];
                $scope.communityLabelList.data.forEach(function(item){
                    var a = {};
                    a.name = item.communityName;
                    a.id = item.objId;
                    selectList.push(a);
                });
                new select({data:selectList,id:"community_select"});
            }
        },500);


        $scope.getType = function() {

            $http.get('http://' + backOfficeHost + '/rest/v1.0/backoffice/dosth/dosthType/', {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                $scope.typeList = data.data;
            }).error(function () {
                $scope.typeList = null;
            });
        };

        $scope.getType();


        $scope.selectType = function () {
            $scope.searchAction();
        };

        $scope.searchAction = function () {

            commId = $("#community_select")[0].dataset.cId;
            $scope.communityId = commId;

            var option = {'communityId': commId};

            if ($scope.type) {
                option.type = $scope.type;
            }

            $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/dosth/dosthTogether/' + $scope.currentPage + '/' + $scope.pageSize, option, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {

                var result = data.data.list;
                $scope.totalPage = data.data.totalPage;
                $scope.list = result;

            }).error(function () {
                $scope.list = null;
            });
        };


        $scope.onSelectPage = function (page) {
            $scope.currentPage = page;
            $scope.searchAction();
        };


        $scope.searchAction();

    }]).controller('dosth_config_Ctrl', ['$scope','$http', '$location', function ($scope,  $http, $location) {


        $scope.currentPage = 1;
        $scope.totalPage = 10;
        $scope.pageSize = 10;
        $scope.pages = [];


        $scope.requestList = function () {

            var option = {};
            $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/dosth/dosthConfig/' + $scope.currentPage + '/' + $scope.pageSize, option, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {

                var result = data.data.list;
                $scope.totalPage = data.data.totalPage;
                $scope.list = result;

            }).error(function () {
                $scope.list = null;
            });
        };

        $scope.addTypeAction = function (type) {

            var option = {};
            if (type) {
                option = {type: type};
            }else {
                return;
            }

            $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/dosth/dosthConfig/addType/' + $scope.currentPage + '/' + $scope.pageSize, option, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {

                $scope.type = null;
                var result = data.data.list;
                $scope.totalPage = data.data.totalPage;
                $scope.list = result;

            }).error(function () {
                $scope.list = null;
            });
        };

        $scope.deleteTypeAction = function (id) {

            var option = {id: id};
            $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/dosth/dosthConfig/deleteType/' + $scope.currentPage + '/' + $scope.pageSize, option, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {

                var result = data.data.list;
                $scope.totalPage = data.data.totalPage;
                $scope.list = result;

            }).error(function () {
                $scope.list = null;
            });
        };


        $scope.onSelectPage = function (page) {
            $scope.currentPage = page;
            $scope.requestList();
        };

        $scope.requestList();


    }]).directive('pagination', function () {
        return {
            restrict: 'E',
            template: '',
            replace: true,
            link: function (scope, element, attrs) {
                scope.$watch('currentPage', function (value) {
                    scope.pages = [];
                    for (var i = value - 5 > 0 ? value - 5 : 1; i <= (value + 5 > scope.totalPage ? scope.totalPage : value + 5); i++) {
                        scope.pages.push(i);
                    }
                });
                scope.$watch('totalPage', function (value) {
                    scope.pages = [];
                    for (var i = scope.currentPage - 5 > 0 ? scope.currentPage - 5 : 1; i <= (scope.currentPage + 5 > value ? value : scope.currentPage + 5); i++) {
                        scope.pages.push(i);
                    }
                });

                scope.isActive = function (page) {
                    return scope.currentPage === page;
                };
                scope.selectPage = function (page) {
                    if (!scope.isActive(page)) {
                        scope.currentPage = page;
                        scope.onSelectPage(page);
                    }
                };
                scope.selectPrevious = function () {
                    if (!scope.noPrevious()) {
                        scope.selectPage(scope.currentPage - 1);
                    }
                };
                scope.selectNext = function () {
                    if (!scope.noNext()) {
                        scope.selectPage(scope.currentPage + 1);
                    }
                };
                scope.noPrevious = function () {
                    return scope.currentPage == 1;
                };
                scope.noNext = function () {
                    return scope.currentPage == scope.totalPage;
                };
            }
        };
    });
